import 'dart:convert' as convert;

import 'package:blue_plugin/blue_plugin.dart';
import 'package:blue_plugin/utils/toast_util.dart';
import 'package:blue_plugin_example/base/base_blue_controller.dart';
import 'package:blue_plugin_example/bean/dev_info_init.dart';
import 'package:blue_plugin_example/bean/niaoye_dev_bean.dart';
import 'package:blue_plugin_example/res/colors.dart';

class PlugRouteNiaoYeSDKController extends BaseBlueController {
  String tabURO = "-";
  String tabLEU = "-";
  String tabNIT = "-";
  String tabBLD = "-";
  String tabGLU = "-";
  String tabSG = "-";
  String tabBIL = "-";
  String tabPRO = "-";
  String tabVC = "-";
  String tabKET = "-";
  String tabPH = "-";

  String xtEdit = "";
  var btnColor = ColorStyle.color_btn_bg_2;
  String connectMsg = "连接";
  String radioGroupValue = "空腹血糖";

  @override
  void onInit() {
    super.onInit();
    baseGetTime();
  }

  ///1.扫描设备
  onConnectBtn() {
    clearEditInfo();
    baseGetTime();
    blueInstance!.sdkDevInit(DevInfoInit.DEV_NIAOYE_NAME);
    blueInstance!.sdkDevConnectBtn(
        DevInfoInit.DEV_NIAOYE_NAME,
        (devType, json) => {
              print("设备${devType}--数据${json}"),
              valueOpt(convert.jsonDecode(json)),
            },
        (devType, state, msg) => {
              connectMsg = msg,
              update(),
            });
  }

  ///2.根据设备类型 子类做处理
  valueOpt(Map<String, dynamic> json) {
    dynamic displayDataBean = NiaoyeDevBean.fromJson(json);
    String? URO = displayDataBean.uRO!.internationUnit;
    tabURO = getValueReplace(URO);
    String? LEU = displayDataBean.lEU!.internationUnit;
    tabLEU = getValueReplace(LEU);
    String? NIT = displayDataBean.nIT!.internationUnit;
    tabNIT = getValueReplace(NIT);
    String? BLD = displayDataBean.bLD!.internationUnit;
    tabBLD = getValueReplace(BLD);

    String? GLU = displayDataBean.gLU!.internationUnit;
    tabGLU = getValueReplace(GLU);
    String? SG = displayDataBean.sG!.internationUnit;
    tabSG = getValueReplace(SG);
    String? BIL = displayDataBean.bIL!.internationUnit;
    tabBIL = getValueReplace(BIL);
    String? PRO = displayDataBean.pRO!.internationUnit;
    tabPRO = getValueReplace(PRO);
    String? VC = displayDataBean.vC!.internationUnit;
    tabVC = getValueReplace(VC);
    String? KET = displayDataBean.kET!.internationUnit;
    tabKET = getValueReplace(KET);
    String? PH = displayDataBean.pH!.internationUnit;
    tabPH = getValueReplace(PH);
  }
  ///3销毁连接对象
  sdkDestroyBlue(String type){
    blueInstance!.sdkDestroyBlue(type);
  }

  bool getIsEmpty(String zb) {
    if (zb.isEmpty ||
        "0" == zb ||
        "-" == zb ||
        zb.contains("Norm") ||
        "NULL" == zb) {
      return true;
    } else {
      return false;
    }
  }

  String getValueReplace(String? msg) {
    if (msg!.isEmpty) {
      return "-";
    } else {
      String replace = msg
          .replaceAll("mmol/L", "")
          .replaceAll("g/L", "")
          .replaceAll("umol/L", "")
          .replaceAll("cells/uL", "")
          .replaceAll("cells/ul", "");
      if ("0" == replace) {
        return "-";
      } else {
        return replace;
      }
    }
  }

  String getValue(String? msg) {
    if (msg!.isEmpty) {
      return msg;
    } else {
      if ("-" == getValueReplace(msg)) {
        return "-";
      } else {
        return msg;
      }
    }
  }

  onRadioClick(value) {
    radioGroupValue = value;
    clearEditInfo();
  }

  onUpDataBtn() {
    ToastUtils.showError("提交");
  }

  setBtnbg(bool flag) {
    if (flag) {
      btnColor = ColorStyle.color_btn_bg;
    } else {
      btnColor = ColorStyle.color_btn_bg_2;
    }
    update();
  }

  void clearEditInfo() {
    tabURO = "-";
    tabLEU = "-";
    tabNIT = "-";
    tabBLD = "-";
    tabGLU = "-";
    tabSG = "-";
    tabBIL = "-";
    tabPRO = "-";
    tabVC = "-";
    tabKET = "-";
    tabPH = "-";
    xtEdit = "";
    update();
  }

  void setEdit(data) {
    xtEdit = data.toString();
    update();
  }
}
